#include "systemcontroller.h"

/*!
\class Athlete
Class for Manageing the System
*/

/*!
  \fn SystemController
  Constructor with no Params
*/
SystemController::SystemController()
{
    this->databasePath="";
    this->sqlLitedatabaseManager = new SqlLite();
    this->settings = new QSettings("TrainerManagement", "TrainerManagement");
}

/*!
  \fn ~SystemController
  Destructor with no Params
*/
SystemController::~SystemController()
{
    delete this->sqlLitedatabaseManager;
    delete this->settings;
}


/*!
  \fn checkDatabase
  Check if the Database is ok
  \returns an String with the error if an error occured, else an Empty string
*/
QString SystemController::checkDatabase()
{
    QString error = "";
    if (!this->sqlLitedatabaseManager->checkDatabase(this->databasePath))
        error = this->sqlLitedatabaseManager->getLasterErrorMessage();
    this->sqlLitedatabaseManager->disconnect();

    return error;

}

/*!
  \fn showErrorDialog
  Shows an Error Dialog
  \param parent: The parent widget
  \param title: The title of the error Message
  \param text: the text of the Dialog
  \param error: An extended error Message
*/
void SystemController::showErrorDialog(QWidget *parent, QString title, QString text, QString error)
{
    QMessageBox msgBox(parent);
    msgBox.setWindowTitle(title);
    msgBox.setText(text);
    msgBox.setIcon(QMessageBox::Critical);
    if (error != "")
        msgBox.setDetailedText(error);
    msgBox.exec();
}


/////////////////////
//Setter Functions//
///////////////////
void SystemController::setDatabsePath(QString path)
{
    this->databasePath = path;
}

void SystemController::addRecentlyUsedDbs(QString path)
{
    QStringList recDbs(this->settings->value("recentlyuseddbs").toStringList());
    if (recDbs.size() >= 10)
    {
        recDbs.removeLast();
    }
    recDbs.prepend(path);
    recDbs.removeDuplicates();
    this->settings->setValue("recentlyuseddbs", recDbs);
}

void SystemController::setLanguage(QString countryCode)
{
    this->language = countryCode;
}






/////////////////////
//Getter Functions//
///////////////////
QString SystemController::getDatabasePath()
{
    return this->databasePath;
}

QStringList SystemController::getRecentlyUsedDbs()
{
    return this->settings->value("recentlyuseddbs").toStringList();
}

QString SystemController::getLanguage()
{
    return this->language;
}

QSettings *SystemController::getSettings()
{
    return this->settings;
}



/////////////////////////
//Translator Functions//
///////////////////////

QString SystemController::tranlsateGender(bool male)
{
    if (male)
        return QObject::tr("male");
    else
        return QObject::tr("female");
}
